# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=icu

pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug-libs")
pkgver=53.1
pkgrel=4
arch=('any')
pkgdesc="International Components for Unicode library (mingw-w64)"
arch=('any')
url="http://www.icu-project.org/"
license=('custom:"icu"')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
# There's no point adding 'debug' to options as both
# debug and release packages are built regardless.
options=('!buildflags' 'staticlibs' 'strip')
source=("http://download.icu-project.org/files/icu4c/${pkgver}/icu4c-${pkgver//./_}-src.tgz"
    0004-move-to-bin.mingw.patch
    0007-actually-move-to-bin.mingw.patch
    0008-data-install-dir.mingw.patch
    0009-fix-bindir-in-config.mingw.patch
    0010-msys-rules-for-makefiles.mingw.patch
    0011-sbin-dir.mingw.patch
    0012-libprefix.mingw.patch
    0014-mingwize-pkgdata.mingw.patch
    0015-debug.mingw.patch
    0016-icu-pkgconfig.patch
    )
md5sums=('b73baa6fbdfef197608d1f69300919b9'
         '4c8143786ed499184e64fc5a1a1d0fdf'
         'eb56b72df2ca0dc77261c4f2adc5c8ad'
         '9c63125f7bdd88871c89e13ee62ac02b'
         '93830d77021eab270e537d466653557e'
         'cce2427d95bcb4a364df5ab605c3e9a3'
         'ee93f131a132f7e65d11d15a467b7abc'
         '35ece84824fc66f2833f9f4543616e8a'
         'e191e78207b55da374b6a8200f48da5b'
         'd1accfbefbcd399a4a28a4a4e0737915'
         '4e2272dc17e6cfd33f1dd1030f3f50ec')
prepare() {
  cd "$srcdir/icu"

  patch -p1 -i "$srcdir"/0004-move-to-bin.mingw.patch
  patch -p1 -i "$srcdir"/0007-actually-move-to-bin.mingw.patch
  patch -p1 -i "$srcdir"/0008-data-install-dir.mingw.patch
  patch -p1 -i "$srcdir"/0009-fix-bindir-in-config.mingw.patch
  patch -p1 -i "$srcdir"/0010-msys-rules-for-makefiles.mingw.patch
  patch -p1 -i "$srcdir"/0011-sbin-dir.mingw.patch
  patch -p1 -i "$srcdir"/0012-libprefix.mingw.patch
  patch -p1 -i "$srcdir"/0014-mingwize-pkgdata.mingw.patch
  patch -p1 -i "$srcdir"/0015-debug.mingw.patch
  patch -p1 -i "$srcdir"/0016-icu-pkgconfig.patch

  cd source
  autoreconf -vfi
}

build() {
  local -a extra_config
  cd "$srcdir/icu/"
  # For ICU we ignore the options for debug above and always
  # build both debug and release (and static and shared).
  # This is because ICU people want the debug libraries be
  # suffixed with 'd' and e.g. Qt-Project go along with this
  # (though only for the static variant, oddly, see:
  #   qtbase/config.tests/unix/icu/icu.pro
  for _variant in debug release; do
    [ -d build-${CARCH}-${_variant} ] && rm -rf build-${CARCH}-${_variant}
    if [ "${_variant}" = "debug" ]; then
      extra_config=( --enable-debug )
      extra_config+=( --with-library-suffix=d )
    else
      extra_config=( --enable-release )
    fi
    cp -rf source build-${CARCH}-${_variant}
    pushd build-${CARCH}-${_variant}
    ./configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST} \
      --disable-rpath \
      --enable-shared \
      --enable-static \
      "${extra_config[@]}"
    make
    popd
  done
}

check() {
  for _variant in debug release; do
    pushd "$srcdir/icu/build-${CARCH}-${_variant}"
    PATH=$srcdir/icu/build-${CARCH}/bin:$srcdir/icu/build-${CARCH}/lib:$PATH \
    make V=1 -k check
    popd
  done
}

package_icu() {
  pushd "$srcdir/icu/build-${CARCH}-release"
  make install DESTDIR="$pkgdir"
  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  mv "${pkgdir}${MINGW_PREFIX}"/bin/*.a "${pkgdir}${MINGW_PREFIX}"/lib/
}

package_icu-debug-libs() {
  options=('debug' '!strip')
  pushd "$srcdir/icu/build-${CARCH}-debug"
  make install DESTDIR="$pkgdir"
  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  mv "${pkgdir}${MINGW_PREFIX}"/bin/*.a "${pkgdir}${MINGW_PREFIX}"/lib/
  # Remove bits that are also in the main package.
  # TODO :: Maybe move the pkg-config files to 'd' suffixed variants?
  rm -rf "${pkgdir}${MINGW_PREFIX}"/{include,sbin,share,lib/pkgconfig}
  rm -rf "${pkgdir}${MINGW_PREFIX}"/bin/*.exe
  rm -rf "${pkgdir}${MINGW_PREFIX}"/bin/icu-config
}

package_mingw-w64-i686-icu() {
  package_icu
}

package_mingw-w64-i686-icu-debug-libs() {
  package_icu-debug-libs
}

package_mingw-w64-x86_64-icu() {
  package_icu
}

package_mingw-w64-x86_64-icu-debug-libs() {
  package_icu-debug-libs
}
